/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef NODE_UTIL_LOCKEDQUEUE_H_
#define NODE_UTIL_LOCKEDQUEUE_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <deque>
#include "node/system/lockguard.h"

namespace node {
    namespace util {

        template < class T, class LockType, typename StorageType = std::deque<T> >
        class LockedQueue
        {
        public:
            LockedQueue()
                : canceled_(false)
            {
            }
            virtual ~LockedQueue()
            {
            }


            void add(const T& item)
            {
                ::node::system::LockGuard<LockType> guard(lock_);
                queue_.push_back(item);
            }

            bool next(T& result)
            {
                ::node::system::LockGuard<LockType> guard(lock_);
                if (!guard.isLocked()) {
                    return false;
                }

                if (queue_.empty())
                    { return false; }

                result = queue_.front();
                queue_.pop_front();

                return true;
            }

            template<class Checker>
            bool next(T& result, Checker& check)
            {
                ::node::system::LockGuard<LockType> guard(lock_);
                if (!guard.isLocked()) {
                    return false;
                }

                if (queue_.empty())
                    { return false; }

                result = queue_.front();
                if (!check.process(result))
                    { return false; }

                queue_.pop_front();
                return true;
            }

            T& peek()
            {
                lock();
                T& result = queue_.front();
                return result;
            }

            void cancel()
            {
                ::node::system::LockGuard<LockType> guard(lock_);
                canceled_ = true;
            }

            bool cancelled()
            {
                ::node::system::LockGuard<LockType> guard(lock_);
                return canceled_;
            }

            void lock()
            {
                this->lock_.lock();
            }

            void unlock()
            {
                this->lock_.unlock();
            }

            bool empty()
            {
                ::node::system::LockGuard<LockType> guard(lock_);
                return queue_.empty();
            }

        private:
            LockType lock_;
            StorageType queue_;
            volatile bool canceled_;
        };

    }
}

#endif // NODE_UTIL_LOCKEDQUEUE_H_
